package com.itqf.controller;

import com.itqf.utils.R;
import org.apache.commons.io.IOUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Description:
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/11/17
 * @Time: 上午10:11
 */
@Controller
public class FileController {

    /**
     * 处理文件上传
     * @return
     */
    @RequestMapping("/uploadFile")
    // <input type="text" name="photo">
    //MultipartFile photo  代表的就是上传的文件
    @ResponseBody
    public Map<String,Object> upload(MultipartFile photo, HttpServletRequest request){
        try {
            String name = photo.getOriginalFilename();//得到上传的文件的名称
            long len =  photo.getSize();
            System.out.println(name+"---"+len);
            //存储文件
            //得到存储图片的文件夹
            String path = request.getServletContext().getRealPath("/photo");
            File dir = new File(path);
            if (!dir.exists()){
                dir.mkdir();
            }
            //得到文件的后缀   jdisjdis.xxx.jpg
            String suffix = name.substring(name.lastIndexOf("."));
            //得到新文件的名称
            name = UUID.randomUUID().toString()+suffix;

            photo.transferTo(new File(dir,name));

            return R.ok();// {"code":0}
        } catch (IOException e) {
            e.printStackTrace();
        }

        return R.error();//{"code":1}


    }

    @RequestMapping("/uploadFile1")
    //MultipartFile photo  代表的就是上传的文件
    @ResponseBody
    public Map<String,Object> uploadFile1(MultipartFile photo[], HttpServletRequest request){
        try {
            for (MultipartFile file : photo) {
                String name = file.getOriginalFilename();//得到上传的文件的名称
                long len =  file.getSize();//得到上传的文件的大小
                System.out.println(name+"---"+len);
                //存储文件
                //得到存储图片的文件夹
                String path = request.getServletContext().getRealPath("/photo");
                File dir = new File(path);
                if (!dir.exists()){
                    dir.mkdir();
                }
                //得到文件的后缀   jdisjdis.xxx.jpg
                String suffix = name.substring(name.lastIndexOf("."));
                //得到新文件的名称
                name = UUID.randomUUID().toString()+suffix;

                file.transferTo(new File(dir,name));
            }

            return R.ok();// {"code":0}
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }


    //文件下载
    @RequestMapping("/downloadFile")
    public  void  download(HttpServletResponse response){
        try {
            String filePath = "/Users/liliting/Desktop/part3/_18SPringMVCDemo02/src/main/resources/girl.jpg";
            String suffix = filePath.substring(filePath.lastIndexOf("."));
            //文件下载 响应头
            response.setHeader("content-disposition","attachment;filename="+UUID.randomUUID().toString()+suffix);
            IOUtils.copy(new FileInputStream(new File(filePath)),response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

    }



}
